package cn.snowheart.boot.launcher;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@SpringBootApplication
public class SpringBootLauncherApplication {

    private Logger logger = LoggerFactory.getLogger(SpringBootLauncherApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(SpringBootLauncherApplication.class, args);
    }

    /**
     * Reactive式Endpoint案例
     */
    @Bean
    public RouterFunction<ServerResponse> helloWorld() {
        return route(GET("/hello-world"),
                request -> ok().body(Mono.just("Hello,world~"), String.class)
        );
    }

    /**
     * 通过事件监听器来查看具体的WebServer实现类
     *
     * @param event 容器启动完毕后的监听事件
     */
    @EventListener(WebServerInitializedEvent.class)
    public void onWebServerReady(WebServerInitializedEvent event) {
        logger.info("WebServer Implement Class: {}",
                event.getWebServer().getClass().getName());
    }

}
